package com.fanwh.sort.SelectSort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
 * @program: DataStructure
 * @description: 选择排序
 * @author: Fanwh
 * @create: 2020-09-23 17:34
 **/
public class SelectSort {

    public static void SelectSort(int[] arr){
        for (int i = 0; i < arr.length-1; i++) {
            int minIndex = i;
            int min = arr[i];
            for (int j = i+1; j < arr.length; j++) {
                if(min > arr[j]){//找到最小值
                    min = arr[j];
                    minIndex = j;
                }
            }
            if(minIndex != i){ //交换
                arr[minIndex] = arr[i];
                arr[i] = min;
            }
        }
    }
    public static void main(String[] args) {
        int[] arr = {101,34,119,1};
        System.out.println("排序前：");
        System.out.println(Arrays.toString(arr));
        SelectSort(arr);
        System.out.println("排序后：");
        System.out.println(Arrays.toString(arr));

       /* //测试
        int[] arr = new int[80000];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int)(Math.random()*80000);
        }
        Date date1 = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String datestr1 = simpleDateFormat.format(date1);
        System.out.println("排序之前时间："+datestr1);

        SelectSort(arr);

        Date date2 = new Date();
        String datestr2 = simpleDateFormat.format(date2);
        System.out.println("排序之后时间："+datestr2);*/
    }
}
